package com.lulufind.lulu.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.lulufind.lulu.model.Demand;
import com.lulufind.lulu.model.DemandUserAid;
import com.lulufind.lulu.model.DemandUserOrder;
import com.lulufind.lulu.model.Label;
import com.lulufind.lulu.model.Result;
import com.lulufind.lulu.service.DemandService;
import com.lulufind.lulu.service.LabelService;
import com.lulufind.lulu.vo.DemandInfoVO;
import com.lulufind.lulu.vo.DemandOrderInfoVO;
import com.lulufind.lulu.vo.DemandUserInfoVO;

@Controller
@RequestMapping("/demand")
public class DemandController {
	
	@Autowired
	DemandService demandService;
	@Autowired
	LabelService labelService;
	
	@ResponseBody
	@RequestMapping(value="/addDemand",produces="application/json;charset=UTF-8")
	public String addDemand(Demand demand, String userToken, int[] label){
		
		Result<Demand> rs = demandService.addDemand(demand, userToken, label);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getDemandInfo",produces="application/json;charset=UTF-8")
	public String getDemandInfo(int userId, String userToken, int demandId){
		
		Result<DemandInfoVO> rs = demandService.getDemandInfo(userId, userToken, demandId);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/applyDemandAid",produces="application/json;charset=UTF-8")
	public String applyDemandAid(DemandUserAid demandUserAid, String userToken){
		
		Result<DemandUserAid> rs = demandService.addDemandUserAid(demandUserAid, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/delDemandAid",produces="application/json;charset=UTF-8")
	public String delDemandAid(HttpServletRequest req, int aidUserId, String userToken, int aidDemandId){
		
		Result<DemandUserAid> rs = demandService.delDemandUserAid(aidUserId, userToken, aidDemandId);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addDemandOrder",produces="application/json;charset=UTF-8")
	public String addDemandOrder(DemandUserOrder demandUserOrder, int userId, String userToken){
		
		Result<Integer> rs = demandService.addDemandOrder(demandUserOrder, userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/completeDemandOrder",produces="application/json;charset=UTF-8")
	public String completeDemandOrder(int orderId, int userId, String userToken){
		
		Result<DemandUserOrder> rs = demandService.completeDemandOrder(orderId, userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/commentDemandOrder",produces="application/json;charset=UTF-8")
	public String commentDemandOrder(int orderId, String orderComment, int userId, String userToken){
		
		Result<DemandUserOrder> rs = demandService.commentDemandOrder(orderId, orderComment, userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/getAllLabels",produces="application/json;charset=UTF-8")
	public String getAllLabels(){
		
		Result<Map<String, List<Label>>> rs = labelService.getAllLabels();
		
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getHotLabels",produces="application/json;charset=UTF-8")
	public String getHotLabels(int start, int num){
		
		Result<Set<Label>> rs = labelService.getHotLabels(start, num);
		
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getNearbyDemands",produces="application/json;charset=UTF-8")
	public String getNearbyDemands(int userId, String userToken, double userLat, double userLng, double rangeLat, double rangeLng){
		
		Result<List<DemandInfoVO>> rs = demandService.getNearbyDemands(userId, userToken, userLat, userLng, rangeLat, rangeLng);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getUserDemands",produces="application/json;charset=UTF-8")
	public String getUserDemands(int userId, String userToken, int targetUserId){
		
		Result<List<DemandInfoVO>> rs = demandService.getUserDemands(userId, userToken, targetUserId);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getDemandAidUserInfo",produces="application/json;charset=UTF-8")
	public String getDemandAidUserInfo(Integer userId, String userToken, Integer demandId){
		
		Result<List<DemandUserInfoVO>> rs = demandService.getDemandAidUserInfo(demandId, userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getAidDemandInfo",produces="application/json;charset=UTF-8")
	public String getAidDemandInfo(Integer userId, String userToken){
		
		Result<List<DemandInfoVO>> rs = demandService.getAidDemandInfo(userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getDemandOrderInfoByAidUser",produces="application/json;charset=UTF-8")
	public String getDemandOrderInfoByAidUser(Integer userId, String userToken, Integer start, Integer num){
		
		Result<List<DemandOrderInfoVO>> rs = demandService.getDemandOrderInfoByAidUser(userId, userToken, start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getDemandOrderInfoByDemandUser",produces="application/json;charset=UTF-8")
	public String getDemandOrderInfoByDemandUser(Integer userId, String userToken, Integer start, Integer num){
		
		Result<List<DemandOrderInfoVO>> rs = demandService.getDemandOrderInfoByDemandUser(userId, userToken, start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getDemandOrderInfo",produces="application/json;charset=UTF-8")
	public String getDemandOrderInfo(Integer userId, String userToken, Integer orderId){
		
		Result<DemandOrderInfoVO> rs = demandService.getDemandOrderInfo(userId, userToken, orderId);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
}
